
package pack;


import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import dao.EmployeeDAORemote;
import dto.EmployeeDTO;

@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable {

	
	private static final long serialVersionUID = 1L;
	
	@EJB
	EmployeeDAORemote employeeDAO;
	
	@ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employeeDTO; // = new EmployeeDTO();
	

	ExternalContext exCon;
	private String uname;
	private String upass;
	
	
	
	public String login() {
		employeeDTO = new EmployeeDTO();
		employeeDTO.setUsername(getUname());
		employeeDTO.setPassword(getUpass());
		
		System.out.println("Picsa!!!" + employeeDTO.getPassword());
		EmployeeDTO eDTO = employeeDAO
				.findEmployeeByUsernameAndPassword(employeeDTO);
		if (eDTO != null) {
			if (eDTO.getPassword() != null)
				if (eDTO.getUsername() != null)
					return "employee";
		}
		else 	if (eDTO == null) {
			System.out.println("Picsafust!!!");
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(null, new FacesMessage("Please enter a valid username and password"));

		}
		return "error";
	}
	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpass() {
		return upass;
	}

	public void setUpass(String upass) {
		this.upass = upass;
	}

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}
	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}
}





/*
@ManagedBean(name = "loginBean")
@RequestScoped
public class LoginBean implements Serializable {

	
	private static final long serialVersionUID = 1L;
	
	
	
	private String uname;
	private String upass;
	
	
	
	public String login() {
		return "error";
	}
	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpass() {
		return upass;
	}

	public void setUpass(String upass) {
		this.upass = upass;
	}

}
*/